<div align="left"><b><?=translate('title');?></b><br><br></div>
<?
error_reporting(0);
include('wallet_db.html');
$now_date=date("l, F j, Y \a\\t g:i A",strtotime("-2 hours")).", GMT";

$database_error=translate('database_error');
$database_up=translate('database_up');
$ip_not_real=translate('ip_not_real');
$title=translate('title');
$demo_mode=translate('demo_mode');
$demo_mode_on=translate('demo_mode_on');
$demo_mode_off=translate('demo_mode_off');
$addresses_allowed=translate('addresses_allowed');
$delete=translate('delete');
$ipempty=translate('ipempty');
$new_ip_addr=translate('new_ip_addr');
$submit=translate('submit');
$api_md5=translate('api_md5');
$line1=translate('line1');
$ref_link=translate('ref_link');
$here=translate('here');
$more=translate('more');
$api_doc_p=translate('api_doc_p');
$next=translate('next');
$line_end=translate('line_end');
$upg=translate('upg');
$disp_u_on=translate('disp_u_on');
$disp_u_off=translate('disp_u_off');
$display_upgrades_example=translate('display_upgrades_example');

$params[sid] = $sid;
$params[action] = "editApiSettings";
$url = "$baseURL/$webDIR/?".encode_params($params);

//if($errorMsg) {
//include ('errorMsg.html');
//}
if($okMsg) {
include ('okMsg.html');
}
else {
}

$params[sid] = $sid;
$params[action] = "walletAccountRefill";
$url1 = "$baseURL/$webDIR/?".encode_params($params);

$params[sid] = $sid;
$params[action] = "downloadApiDemo";
$url2 = "$baseURL/$webDIR/?".encode_params($params);


echo "<div align=left>$line1 $ref_link <a href=\"$url1\">$here</a>. $more <a
href=\"$url2\">$api_doc_p</a>. $next<br><br>$line_end</div><br><br>";

if (isset($subaction) && $subaction=='delete') {
	$old_dbh=$dbh;
	$dbh=$dbh81;
	//------------------------------------------------------
	        $sql['type']='SELECT';
	        $sql['params']='*';
	        $sql['table']='api_settings';
	        $sql['where']="reseller_id='$id'";
	        $sql['options']='';
	        $sql['return']='struct';
        //------------------------------------------------------
	if (!$result=sql_exec($sql,$dbh81,'assoc')) {
					$errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
		//exit("<h4><font color=red>$database_error</font></h4>");
        } else {
		$addr=$result[0][ip_addr];
		$addr=explode(",",$addr);
                foreach ($addr as $value) {
                        if ($value != $addr2del) $new_addr[]=$value;
                }
                if (!empty($new_addr)) {
                        $new_addr=implode(",",$new_addr);
                } else {
                        $new_addr='';
                }
		//------------------------------------------------------
	                $sql['type']='UPDATE';
	                $sql['params']="ip_addr='$new_addr'";
	                $sql['table']='api_settings';
	                $sql['where']="reseller_id='$id'";
	                $sql['options']='';
	                $sql['return']='';
	        //------------------------------------------------------
		if (!$result=sql_exec($sql,$dbh81)) {
					$errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
       	//exit("<h4><font color=red>$database_error</font></h4>");
        	} else {
				$okMsg = translate('database_up');
				include_once('okMsg.html');
			//echo "<h4><font color=green>$database_up</font></h4>";

//------------------------------------------------------
		                $sql['type']='SELECT';
		                $sql['params']='name, first_name, last_name,
email';
		                $sql['table']='resellers';
		                $sql['where']="reseller_id='$id'";
		                $sql['options']='';
		                $sql['return']='struct';

//------------------------------------------------------
	                if (!$result=sql_exec($sql,$dbh81)) {
					$errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
                	}
                	$fullname=$result[0][first_name]."
".$result[0][last_name];
                	$email[address]=$result[0][email];
                	$nick_name=$result[0][name];


//------------------------------------------------------
		                $sql['type']='SELECT';
		                $sql['params']='*';
		                $sql['table']='api_settings';
		                $sql['where']="reseller_id='$id'";
		                $sql['options']='';
		                $sql['return']='struct';

//------------------------------------------------------
		        if (!$result=sql_exec($sql,$dbh81,'assoc')) {
					$errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
			}
			$addr=$result[0][ip_addr];
			if (empty($addr)) {
				$addr='You have not registered an IP address yet';
			} else {
				$addr=preg_replace("/,/","\n",$addr);
			}

                	$SETTINGS["baseTMName"]="ResselersPanel Team";

$SETTINGS["baseSupportEmail"]="support@resellerspanel.com";
	
	                $mail_array= getMail ("api_ip_deleted", Array(
	                                "fullname"=>$fullname,
	                                "ip2delete"=>$addr2del,
					"date"=>$now_date,
                                	"username"=>$nick_name,
	                                "ip_list"=>$addr,
	                                "mainserver" => "ResellersPanel"
	                                ),
Array("HEADER"=>1,"BODY"=>1,"FOOTER"=>1), "resellers_id", $id,'mail');
	
	                $email[from]= $mail_array[from];
	                $email[mime]= $mail_array[mime];
	                $email[subject]= $mail_array[subject];
	                $email[body]=$mail_array[body];
			if
(!mail($email[address],$email[subject],$email[body],
				"From: ".$email[from]."\r\n"
                                ."Content-type: ".$email[mime]."; charset=iso-8859-1\r\n")) {
					$mail=serialize($email);
					mail('paz@bastun.net','error with api settings update',"$email",'From: paz@bastun.net');
			}
		}
	}
unset($sql);
}
$old_dbh=$dbh;
$dbh=$dbh81;
if (isset($_POST[hid_var]) && $_POST[hid_var]==345) {
	if (!empty($_POST[address]) && !preg_match("/^(\d{1,3}\.){3}\d{1,3}$/", $_POST[address])) {

		$ipto = $_POST[address].$mofo;
		translate('ip_not_real' , array('notrealip' => $ipto));
		include_once('errorMsg.html');
		//echo $_POST[address].$mofo;
		//echo "<h4><font color=red>$_POST[address] $ip_not_real</font></h4>";

	} else {
		
		//------------------------------------------------------
			$sql['type']='SELECT';
			$sql['params']='name, first_name, last_name, email';
			$sql['table']='resellers';
			$sql['where']="reseller_id='$id'";
			$sql['options']='';
			$sql['return']='struct';
		//------------------------------------------------------
		if (!$result=sql_exec($sql,$dbh81)) {
					$errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
		}
		$fullname=$result[0][first_name]." ".$result[0][last_name];
		$email[address]=$result[0][email];
		$nick_name=$result[0][name];
		$query="SELECT * FROM api_settings WHERE reseller_id='$id'";
		if (!$result=pg_query($dbh81,$query)) {
					$errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
		}
		$num=pg_num_rows($result);
		if ($num!=0) {

$current_dmode=pg_fetch_result($result,0,'demo_mode');
			$current_addr=pg_fetch_result($result,0,'ip_addr');

$current_upgrade=pg_fetch_result($result,0,'upgrade');
			if (!empty($_POST[address]) &&
!ereg($_POST[address],$current_addr)) {
				if (empty($current_addr)) {

//------------------------------------------------------
						$sql['type']='UPDATE';

$sql['params']="ip_addr='$_POST[address]'";

$sql['table']='api_settings';

$sql['where']="reseller_id='$id'";
						$sql['options']='';
						$sql['return']='struct';

//------------------------------------------------------
					if
(!$result=sql_exec($sql,$dbh81,'assoc')) {
					$errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
					}
				} else {

$addr=$current_addr.",$_POST[address]";

//------------------------------------------------------
						$sql['type']='UPDATE';

$sql['params']="ip_addr='$addr'";

$sql['table']='api_settings';

$sql['where']="reseller_id='$id'";
						$sql['options']='';
						$sql['return']='struct';

//------------------------------------------------------
					if
(!$result=sql_exec($sql,$dbh81,'assoc')) {
					$errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
					}
				}

//------------------------------------------------------
					$sql['type']='SELECT';
                        		$sql['params']='*';
                        		$sql['table']='api_settings';
                        		$sql['where']="reseller_id='$id'";
                        		$sql['options']='';
                        		$sql['return']='struct';

//------------------------------------------------------
               			if (!$result=sql_exec($sql,$dbh81,'assoc'))
{
					$errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
                		}
                		$addr=$result[0][ip_addr];
                		if (empty($addr)) {
                		        $addr='none';
                		} else {

$addr=preg_replace("/,/","\n",$addr);
                		}

                		$SETTINGS["baseTMName"]="ResselersPanel
Team";

$SETTINGS["baseSupportEmail"]="support@resellerspanel.com";

                		$mail_array= getMail ("api_ip_added", Array(
                		                "fullname"=>$fullname,
                		                "ip"=>$_POST[address],
                		                "ip_list"=>$addr,
                		                "username"=>$nick_name,
                		                "date"=>$now_date,
                		                "mainserver" =>"ResellersPanel"
                		                ),
Array("HEADER"=>1,"BODY"=>1,"FOOTER"=>1), "resellers_id", $id,'mail');
		
		                $email[from]= $mail_array[from];
		                $email[mime]= $mail_array[mime];
		                $email[subject]= $mail_array[subject];
		                $email[body]=$mail_array[body];
		                if
(!mail($email[address],$email[subject],$email[body],
		                        "From: ".$email[from]."\r\n"
		                        ."Content-type: ".$email[mime].";
charset=iso-8859-1\r\n")) {
		                                $mail=serialize($email);
		                                mail('paz@bastun.net','error
with api settings update',"$email",'From: paz@bastun.net');
				}
			}
			if ($_POST[radio]!="$current_dmode") {

//------------------------------------------------------
					$sql['type']='UPDATE';

$sql['params']="demo_mode='$_POST[radio]'";
					$sql['table']='api_settings';
					$sql['where']="reseller_id='$id'";
					$sql['options']='';
					$sql['return']='struct';

//------------------------------------------------------
				if (!$result=sql_exec($sql,$dbh81,'assoc'))
{
					$errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
				}
				if ($_POST[radio]=='f') {
					$demo='off';
				} elseif ($_POST[radio]=='t') {
					$demo='on';
				}
				$SETTINGS["baseTMName"]="ResselersPanel
Team";

$SETTINGS["baseSupportEmail"]="support@resellerspanel.com";

				$mail_array= getMail ("api_demo_changed",
Array(
						"fullname"=>$fullname,
						"demo"=>$demo,
						"username"=>$nick_name,
						"date"=>$now_date,
						"mainserver" =>
"ResellersPanel"
						),
Array("HEADER"=>1,"BODY"=>1,"FOOTER"=>1), "resellers_id", $id,'mail');

				$email[from]= $mail_array[from];
				$email[mime]= $mail_array[mime];
				$email[subject]= $mail_array[subject];
				$email[body]=$mail_array[body];
				if
(!mail($email[address],$email[subject],$email[body],
					"From: ".$email[from]."\r\n"
					."Content-type: ".$email[mime].";
charset=iso-8859-1\r\n")) {
						$mail=serialize($email);
						mail('paz@bastun.net','error
with api settings update',"$email",'From: paz@bastun.net');
				}
			}
			if ($_POST[upgrade]!="$current_upgrade") {

//------------------------------------------------------
                                        $sql['type']='UPDATE';

$sql['params']="upgrade='$_POST[upgrade]'";
                                        $sql['table']='api_settings';
                                        $sql['where']="reseller_id='$id'";
                                        $sql['options']='';
                                        $sql['return']='struct';

//------------------------------------------------------
                                if (!$result=sql_exec($sql,$dbh81,'assoc'))
{

					$errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
                                }
                                if ($_POST[upgrade]=='0') {
                                        $demo='off';
                                } elseif ($_POST[upgrade]=='1') {
                                        $demo='on';
                                }
                                $SETTINGS["baseTMName"]="ResselersPanel
Team";

$SETTINGS["baseSupportEmail"]="support@resellerspanel.com";

                                $mail_array= getMail ("api_upgrade_changed",
Array(
                                                "fullname"=>$fullname,
                                                "demo"=>$demo,
                                                "username"=>$nick_name,
                                                "date"=>$now_date,
                                                "mainserver" =>
"ResellersPanel"
                                                ),
Array("HEADER"=>1,"BODY"=>1,"FOOTER"=>1), "resellers_id", $id,'mail');

                                $email[from]= $mail_array[from];
                                $email[mime]= $mail_array[mime];
                                $email[subject]= $mail_array[subject];
                                $email[body]=$mail_array[body];
                                if (is_me()) {if
(!mail($email[address],$email[subject],$email[body],
                                        "From: ".$email[from]."\r\n"
                                        ."Content-type: ".$email[mime].";
charset=iso-8859-1\r\n")) {
                                                $mail=serialize($email);
                                                mail('paz@bastun.net','error
with api settings update',"$email",'From: paz@bastun.net');
                                }}
                        }

		} else {
			if (empty($_POST[radio])) $_POST[radio]='f';
			if (empty($_POST[upgrade])) $_POST[upgrade]='1';
			if (!empty($_POST[address])) {
				$par[reseller_id]="$id";
				$par[ip_addr]="$_POST[address]";
				$par[demo_mode]="$_POST[radio]";
				$par[upgrade]="$_POST[upgrade]";

//------------------------------------------------------
					$sql['type']='INSERT';
					$sql['params']=$par;
					$sql['table']='api_settings';
					$sql['where']="";
					$sql['options']='';
					$sql['return']='';

//------------------------------------------------------
				if (!$result=sql_exec($sql,$dbh81)) {
					$errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
				}

//------------------------------------------------------
					$sql['type']='SELECT';
					$sql['params']='*';
					$sql['table']='api_settings';
					$sql['where']="reseller_id='$id'";
					$sql['options']='';
					$sql['return']='struct';

//------------------------------------------------------
				if (!$result=sql_exec($sql,$dbh81,'assoc'))
{
					$errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
				}
				$addr=$result[0][ip_addr];
				if (empty($addr)) {
					$addr='none';
				} else {

$addr=preg_replace("/,/","\n",$addr);
				}

				$SETTINGS["baseTMName"]="ResselersPanel
Team";

$SETTINGS["baseSupportEmail"]="support@resellerspanel.com";

				$mail_array= getMail ("api_ip_added", Array(
                                "fullname"=>$fullname,
                                "ip"=>$_POST[address],
                                "ip_list"=>$addr,
                                "username"=>$nick_name,
                                "date"=>$now_date,
                                "mainserver" => "ResellersPanel"
                                ), Array("HEADER"=>1,"BODY"=>1,"FOOTER"=>1),
"resellers_id", $id,'mail');

				$email[from]= $mail_array[from];
				$email[mime]= $mail_array[mime];
				$email[subject]= $mail_array[subject];
				$email[body]=$mail_array[body];
				if
(!mail($email[address],$email[subject],$email[body],
					"From: ".$email[from]."\r\n"
					."Content-type: ".$email[mime].";
charset=iso-8859-1\r\n")) {
						$mail=serialize($email);
						mail('paz@bastun.net','error
with api settings update',"$email",'From: paz@bastun.net');
				}
				if ($_POST[radio]=='f') {
					$demo='off';
				} elseif ($_POST[radio]=='t') {
					$demo='on';
				}
				$mail_array= getMail ("api_demo_changed",
Array(
						"fullname"=>$fullname,
						"demo"=>$demo,
						"username"=>$nick_name,
						"date"=>$now_date,
						"mainserver" =>
"ResellersPanel"
						),
Array("HEADER"=>1,"BODY"=>1,"FOOTER"=>1), "resellers_id", $id,'mail');

				$email[from]= $mail_array[from];
				$email[mime]= $mail_array[mime];
				$email[subject]= $mail_array[subject];
				$email[body]=$mail_array[body];
				if
(!mail($email[address],$email[subject],$email[body],
					"From: ".$email[from]."\r\n"
					."Content-type: ".$email[mime].";
charset=iso-8859-1\r\n")) {
						$mail=serialize($email);
						mail('paz@bastun.net','error
with api settings update',"$email",'From: paz@bastun.net');
				}
				if ($_POST[upgrade]=='0') {
                                        $demo='off';
                                } elseif ($_POST[upgrade]=='1') {
                                        $demo='on';
                                }
                                $mail_array= getMail ("api_upgrade_changed",
Array(
                                                "fullname"=>$fullname,
                                                "demo"=>$demo,
                                                "username"=>$nick_name,
                                                "date"=>$now_date,
                                                "mainserver" =>
"ResellersPanel"
                                                ),
Array("HEADER"=>1,"BODY"=>1,"FOOTER"=>1), "resellers_id", $id,'mail');

                                $email[from]= $mail_array[from];
                                $email[mime]= $mail_array[mime];
                                $email[subject]= $mail_array[subject];
                                $email[body]=$mail_array[body];
                                if (is_me()) {if
(!mail($email[address],$email[subject],$email[body],
                                        "From: ".$email[from]."\r\n"
                                        ."Content-type: ".$email[mime].";
charset=iso-8859-1\r\n")) {
                                                $mail=serialize($email);
                                                mail('paz@bastun.net','error
with api settings update',"$email",'From: paz@b
astun.net');
                                }}

			} else {
				$par[reseller_id]="$id";
				$par[demo_mode]="$_POST[radio]";
				$par[upgrade]="$_POST[upgrade]";

//------------------------------------------------------
					$sql['type']='INSERT';
					$sql['params']=$par;
					$sql['table']='api_settings';
					$sql['where']="";
					$sql['options']='';
					$sql['return']='';

//------------------------------------------------------
				if (!$result=sql_exec($sql,$dbh81)) {
					$errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
				}
				if ($_POST[radio]=='f') {
					$demo='off';
				} elseif ($_POST[radio]=='t') {
					$demo='on';
				}
				$SETTINGS["baseTMName"]="ResselersPanel
Team";

$SETTINGS["baseSupportEmail"]="support@resellerspanel.com";

				$mail_array= getMail ("api_demo_changed",
Array(
						"fullname"=>$fullname,
						"demo"=>$demo,
						"username"=>$nick_name,
						"date"=>$now_date,
						"mainserver" =>
"ResellersPanel"
						),
Array("HEADER"=>1,"BODY"=>1,"FOOTER"=>1), "resellers_id", $id,'mail');

				$email[from]= $mail_array[from];
				$email[mime]= $mail_array[mime];
				$email[subject]= $mail_array[subject];
				$email[body]=$mail_array[body];
				if
(!mail($email[address],$email[subject],$email[body],
					"From: ".$email[from]."\r\n"
					."Content-type: ".$email[mime].";
charset=iso-8859-1\r\n")) {
						$mail=serialize($email);
						mail('paz@bastun.net','error
with api settings update',"$email",'From: paz@bastun.net');
				}
				if ($_POST[upgrade]=='0') {
                                        $demo='off';
                                } elseif ($_POST[upgrade]=='1') {
                                        $demo='on';
                                }
				$mail_array= getMail ("api_upgrade_changed",
Array(
                                                "fullname"=>$fullname,
                                                "demo"=>$demo,
                                                "username"=>$nick_name,
                                                "date"=>$now_date,
                                                "mainserver" =>
"ResellersPanel"
                                                ),
Array("HEADER"=>1,"BODY"=>1,"FOOTER"=>1), "resellers_id", $id,'mail');

                                $email[from]= $mail_array[from];
                                $email[mime]= $mail_array[mime];
                                $email[subject]= $mail_array[subject];
                                $email[body]=$mail_array[body];
                                if (is_me()) {if
(!mail($email[address],$email[subject],$email[body],
                                        "From: ".$email[from]."\r\n"
                                        ."Content-type: ".$email[mime].";
charset=iso-8859-1\r\n")) {
                                                $mail=serialize($email);
                                                mail('paz@bastun.net','error
with api settings update',"$email",'From: paz@bastun.net');
                                }}

			}
		}


        }
}
unset($sql);
$dbh=$old_dbh;
$query="SELECT * FROM api_settings WHERE reseller_id='$id'";
if (!$result=pg_query($dbh81,$query)) {
					$errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
} else {
	$demo=pg_fetch_result($result,0,'demo_mode');
	$upgrade=pg_fetch_result($result,0,'upgrade');
	$ip_addr=pg_fetch_result($result,0,'ip_addr');
        #$ip=preg_replace("/,/",",\n",$ip_addr);
	if (!empty($ip_addr)) $ip=explode(",",$ip_addr);

        echo "<table class='bTable' cellspacing=0>
				
        <form action=\"$url\" method=\"POST\">
        <tr>
                <th colspan=2 align=center>$title</th>
        </tr>
        <tr>
                <td colspan=2>&nbsp;</td>
        </tr>
        <tr>
                <td align=right valign=top width=\"50%\">$demo_mode</td>
                <td align=left valign=top width=\"50%\">$demo_mode_on <input
type=radio name=radio value='t' "; if ($demo=='t') echo "checked"; echo
"><br>
                        $demo_mode_off <input type=radio name=radio
value='f' "; if ($demo=='f') echo "checked"; echo "></td>
        </tr>";
	echo "<tr>
                <td align=right valign=top width=\"50%\">$upg</td>
                <td align=left valign=top width=\"50%\">$disp_u_on: <input
type=radio name=upgrade value='1' "; if ($upgrade=='1') echo "checked"; echo
"><br>
                        $disp_u_off: <input type=radio name=upgrade
value='0' "; if ($upgrade=='0') echo "checked"; echo "></td>
        </tr>";
	echo "<tr>
                <td align=right valign=top
width=\"50%\">$addresses_allowed</td>
                <td align=left valign=top width=\"50%\">";

		if (!empty($ip)) {
			foreach ($ip as $value) {
				$params[sid] = $sid;
				$params[action] = "editApiSettings";
				$params[subaction]="delete";
				$params[addr2del]=$value;
				$url2 =
"$baseURL/$webDIR/?".encode_params($params);
				echo "$value <a
href='$url2'>$delete</a></br>";
			}
		} else {
			echo $ipempty;
		}
		echo "</td>
        </tr>
	<tr>
		<td align=right valign=top width=\"50%\">$new_ip_addr</td>
		<td align=left valign=top width=\"50%\"><input type=text
name=address size=15 maxlength=15></td>
	</tr>
        <tr>
                <td colspan=2 align=center><input type=submit name=submit
value=\"$submit\">
                        <input type=hidden name=hid_var value=345></td>
        </tr>
        </form>
        </table>";

}
$query="SELECT password FROM resellers WHERE reseller_id='$id'";
if (!$result=pg_query($dbh81,$query)) {
					$errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
} else {

        $value=pg_fetch_result($result,0,'password');
				$value=md5($value);
        echo "<p><strong><font color=red size='2'>$api_md5<br>
                $value</font></strong></p>";
}
echo "<p>$display_upgrades_example</p>";
?>
